package decimal;

import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class UseDecimal {

    /**
     * flag = -1,表示bigdemical1 小于 bigdemical2；
     * flag = 0,表示bigdemical1 等于 bigdemical2；
     * flag = 1,表示bigdemical1 大于 bigdemical2；
     */
    @Test
    public void test(){
        BigDecimal bigdemical1 = new BigDecimal("13.14");
        BigDecimal bigdemical2 = new BigDecimal("13.15");
        int result = bigdemical1.compareTo(bigdemical2);
        System.out.println(result);
    }

    /**
     * a.setScale 保留小数方法
     */
    @Test
    public void test2(){
        BigDecimal a = new BigDecimal("12.1456");
        System.out.println(a.scale()); // 4 代表小数点后4位
        BigDecimal b = a.setScale(2, BigDecimal.ROUND_HALF_UP); // 12.15 ROUND_HALF_UP:四合五入
        System.out.println(b);
        System.out.println(a); // a仍旧不会变
    }

    /**
     * stripTrailingZeros()函数就是用于去除末尾多余的0
     * 100.000.stripTrailingZeros() => 1E+2 所以才要用toPlainString
     */
    @Test
    public void test3(){
        BigDecimal a = new BigDecimal("100.000");
        System.out.println(a.stripTrailingZeros().toPlainString());
    }

    @Test
    public void test4(){
        Double d = 3.101;
        boolean result = BigDecimal.valueOf(d).scale() >= 3;
        System.out.println(result);
    }

    @Test
    public void test5(){
        BigDecimal a = BigDecimal.valueOf(12.8123);
        BigDecimal b = BigDecimal.valueOf(1);
        BigDecimal divide = a.divide(b, 0, RoundingMode.HALF_UP);
        System.out.println(divide);
    }

    @Test
    public void test6(){
        BigDecimal bigDecimal = BigDecimal.valueOf(12).multiply(BigDecimal.valueOf(1.1)).setScale(1, RoundingMode.HALF_UP);
        System.out.println(bigDecimal);
    }

    @Test
    public void test7(){
        String number = "10.";
        String[] split = number.split("\\.");
        System.out.println(split);
    }

}